/**********************************************************************/
/*
   Author: Karan Makkar
   Created: October 2023
   Updated: Aug 2025, by Youssef Assarssah
   Description: Effect of Treatment on Report Applying
   Output: TableA8
*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

  clear all
  set more off
  set matsize 11000

* Log
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_reportapply_by_won.txt", text replace

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Set filepaths for dataset
  global aug20 "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta"
  global feb21 "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta"
  global aug21 "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta"

* FIXED EFFECTS: Strata and inputs into PMO-assigned weight interacted by batch
  global absorb absorb(c.stratum_win_prob#batch gender#batch urban#batch ed_cat#batch)

* FIXED EFFECTS (STACKED REGRESSION): Strata, SAKERNAS wave and inputs into PMO-assigned weight interacted by batch and SAKERNAS wave
  global absorb_stacked absorb(c.stratum_win_prob#batch#sak_round gender#batch#sak_round urban#batch#sak_round ed_cat#batch#sak_round)

* SAMPLE RESTRICTIONS  
  global precomma_stack_17 ((sak_round == 5 & inrange(batch, 2, 3)) | (sak_round == 6 & inrange(batch, 2, 11)) | (sak_round == 7 & inrange(batch, 2, 17)))


* SE CLUSTERING (OLS): Individual level
  global vce vce(cluster anon_id4)

* SE CLUSTERING (IV): Individual level
  global cluster cluster(anon_id4)
  
/*----------------------------------------------------------------*/
* SAKERNAS comparison
/*----------------------------------------------------------------*/

  * Load data and append
  use "${aug20}", clear
  append using "${feb21}"
  append using "${aug21}"
  
   *Win before survey var
  gen win_before_survey = .
  replace win_before_survey  = ever_win_3 if sak_round == 5 & inrange(batch, 2, 3)
  replace win_before_survey  = ever_win_11 if sak_round == 6 & inrange(batch, 2, 11)
  replace win_before_survey  = ever_win_17 if sak_round == 7 & inrange(batch, 2, 17)
  gegen win_before_survey = max(win_before_survey), by(anon_id4 sak_round) replace

    *Education cat
  recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

  *Age Cat
  cap drop age_cat
  recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

  * Drop non-randomized batches
  drop if inlist(batch, 1, 15)
  

  *** Regressions

  ** OLS
  * Stacked Sakernas 
   qui summ report_applied if $precomma_stack_17 & win_in_batch == 0
   scalar mean = round(r(mean), 0.001)
   eststo t1_ols: reghdfe report_applied win_in_batch if $precomma_stack_17, $absorb_stacked $vce
   estadd scalar control_mean = mean : t1_ols
   estadd scalar obs = e(N) : t1_ols

   * Aug 20
   qui summ report_applied if inrange(batch, 2, 3) & sak_round == 5 & win_in_batch == 0
   scalar mean = round(r(mean), 0.001)
   eststo t2_ols: reghdfe report_applied win_in_batch if inrange(batch, 2, 3) & sak_round == 5, $absorb $vce
   estadd scalar control_mean = mean : t2_ols
   estadd scalar obs = e(N) : t2_ols

   * Feb 21
   qui summ report_applied if inrange(batch, 2, 11) & sak_round == 6 & win_in_batch == 0
   scalar mean = round(r(mean), 0.001)
   eststo t3_ols: reghdfe report_applied win_in_batch if inrange(batch, 2, 11) & sak_round == 6, $absorb $vce
   estadd scalar control_mean = mean : t3_ols
   estadd scalar obs = e(N) : t3_ols

   * Aug 21
   qui summ report_applied if inrange(batch, 2, 17) & sak_round == 7 & win_in_batch == 0
   scalar mean = round(r(mean), 0.001)
   eststo t4_ols: reghdfe report_applied win_in_batch if inrange(batch, 2, 17) & sak_round == 7, $absorb $vce
   estadd scalar control_mean = mean : t4_ols
   estadd scalar obs = e(N) : t4_ols

   ** IV
   * Stacked Sakernas
   qui summ report_applied if $precomma_stack_17 & win_before_survey == 0
   scalar mean = round(r(mean), 0.001)
   eststo t1_iv: ivreghdfe report_applied (win_before_survey = win_in_batch) if $precomma_stack_17, $absorb_stacked $cluster
   estadd scalar control_mean = mean : t1_iv
   estadd scalar obs = e(N) : t1_iv
   
   * Aug 20
   qui summ report_applied if inrange(batch, 2, 3) & sak_round == 5 & win_before_survey == 0
   scalar mean = round(r(mean), 0.001)
   eststo t2_iv: ivreghdfe report_applied (win_before_survey = win_in_batch) if inrange(batch, 2, 3) & sak_round == 5, $absorb $cluster
   estadd scalar control_mean = mean : t2_iv
   estadd scalar obs = e(N) : t2_iv
   
   * Feb 21
   qui summ report_applied if inrange(batch, 2, 11) & sak_round == 6 & win_before_survey == 0
   scalar mean = round(r(mean), 0.001)
   eststo t3_iv: ivreghdfe report_applied (win_before_survey = win_in_batch) if inrange(batch, 2, 11) & sak_round == 6, $absorb $cluster
   estadd scalar control_mean = mean : t3_iv
   estadd scalar obs = e(N) : t3_iv
   
   * Aug 21
   qui summ report_applied if inrange(batch, 2, 17) & sak_round == 7 & win_before_survey == 0
   scalar mean = round(r(mean), 0.001)
   eststo t4_iv: ivreghdfe report_applied (win_before_survey = win_in_batch) if inrange(batch, 2, 17) & sak_round == 7, $absorb $cluster
   estadd scalar control_mean = mean : t4_iv
   estadd scalar obs = e(N) : t4_iv

   * Label
   label var win_in_batch "Win in Batch"
   label var win_before_survey "Ever Win"

    * Table
  esttab t1_ols t2_ols t3_ols t4_ols t1_iv t2_iv t3_iv t4_iv using $KP_output/tables/descriptive/tex/TableA8.tex, ///
    b(%9.3f) se(%9.3f) ///
    keep(win_in_batch win_before_survey) ///
    collabels(none) ///
    label nonum ///
    star(* 0.10 ** 0.05 *** 0.01) /// 
    mtitles("Stacked" "Aug '20" "Feb '21" "Aug '21" "Stacked" "Aug '20" "Feb '21" "Aug '21") ///
    mgroups("OLS" "IV", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    stats(obs control_mean, label("Observations" "Mean (Control)") fmt(%9.0fc %9.3fc)) ///
    prehead(`"\hline \hline"') ///
    posthead(`"& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\"' `"\hline"') ///    
    prefoot(`"\\ "') ///
    postfoot(`"Batches & 2-14, 16-17 & 2-3 & 2-11 & 2-14, 16-17 & 2-14, 16-17 & 2-3 & 2-11 & 2-14, 16-17 \\"' `"\hline"') ///
    replace
